#include "TRCtrl.h"
//#include "usbreceive.h"
#include "qmath.h"
TRCtrl::TRCtrl(void)
{


}

TRCtrl::~TRCtrl(void)
{


}

bool TRCtrl::Start(void)
{

    bool bSucess=false;
    if(m_hardType==NEWHF)
    {

        bSucess=newHF_RUN(true);
    }
    return bSucess;
}

bool TRCtrl::Stop(void)
{
    bool bSucess=false;
    if(m_hardType==NEWHF)
    {

        bSucess=newHF_RUN(FALSE);
    }
    ResetFIFO(); //ws:清空USB缓存
    return bSucess;
}

void TRCtrl::SetAmp(short ampvalue)
{
   if(m_hardType==NEWHF)
   {

     newHF_SetRFAmp(ampvalue);
   }

}

bool TRCtrl::newHF_RUN(bool bRun)
{
    unsigned char Tmrst;
    if(bRun)	Tmrst = 0xFF;
    else Tmrst = 0;
    return WriteToCtrlEP(VALUE(ADDR_OF_STARTANDSTOP,Tmrst));
}

//设置发射AD9911幅度字
void TRCtrl::newHF_SetRFAmp(short ampval)
{

    BYTE wdbyte[3];

    //	DOUBLE Xishu=8.9478485333333333333333333333333;
    wdbyte[0] = ampval % 256 ;
    wdbyte[1] = (BYTE)(ampval>>8)&0x03 ;
    wdbyte[2] = 6;
    unsigned short nAddress[3];
    bool bSuccess=false;
     nAddress[0] = ADDR_OF_TRFREQ1;
    nAddress[1] = ADDR_OF_TRFREQ2;
    nAddress[2] = ADDR_OF_TRMODE;

    for (int i=0;i<3;i++)
    {
        bSuccess=WriteToCtrlEP(VALUE(nAddress[i],wdbyte[i]));
    }
//	return bSuccess;
}

// 设置频率，fFreq：单位MHz
bool TRCtrl::SetFreq(double fFreq)
{
    bool bSucess=false;
    if(m_hardType==NEWHF)
    {
        bSucess=newHF_SetRFFreq(0.0,198);
        bSucess=newHF_SetLOFreq(0.0,199);

        bSucess=newHF_SetRFFreq(fFreq,2); //2
       bSucess=newHF_SetReceiverFilter(fFreq);
    }
    return bSucess;
}


// 设置发送频率，btType：1：不发射（fFreq=0），2：发射 //1=LO,2=RF
bool TRCtrl::newHF_SetRFFreq(double fFreq, BYTE btType)
{
    unsigned long lFreq;
    int wdbyte[5];

    //	DOUBLE Xishu=8.9478485333333333333333333333333;
    fFreq = fFreq/480*pow(2.0,32)  ;  //491.52 //480
    lFreq = (fFreq+ 0.5) ;
    wdbyte[3] = lFreq % 256 ;
    wdbyte[2] = (lFreq>>8) % 256 ;
    wdbyte[1] = (lFreq>>16) % 256 ;
    wdbyte[0] = (lFreq>>24) ;
    wdbyte[4] = btType;
    unsigned short nAddress[5];
    bool bSuccess=false;


    nAddress[0] = ADDR_OF_TRFREQ4;
    nAddress[1] = ADDR_OF_TRFREQ3;
    nAddress[2] = ADDR_OF_TRFREQ2;
    nAddress[3] = ADDR_OF_TRFREQ1;
    nAddress[4] = ADDR_OF_TRMODE;

    for (int i=0;i<5;i++)
    {
        bSuccess=WriteToCtrlEP(VALUE(nAddress[i],wdbyte[i]));
    }
    return bSuccess;
}

bool TRCtrl::newHF_SetLOFreq(double fFreq, BYTE btType)
{
    //设置本振滤波器
    /////////////////////////////////发射通道/ad9911/////////////////
    double fFreqTemp;
    unsigned long lFreq;
    unsigned char czFreq[5];

    //	DOUBLE Xishu=8.9478485333333333333333333333333;
    fFreqTemp =(fFreq+41.4)/480*pow(2.0,32)  ; //491.52 //480
    lFreq = fFreqTemp+ 0.5 ;
    czFreq[3] = lFreq % 256 ;
    czFreq[2] = (lFreq>>8) % 256 ;
    czFreq[1] = (lFreq>>16) % 256 ;
    czFreq[0] = (lFreq>>24) ;
    czFreq[4] = btType;  //1=LO,2=RF
    unsigned short nAddress[5];
    nAddress[0] = ADDR_OF_LOFREQ4;
    nAddress[1] = ADDR_OF_LOFREQ3;
    nAddress[2] = ADDR_OF_LOFREQ2;
    nAddress[3] = ADDR_OF_LOFREQ1;
    nAddress[4] = ADDR_OF_LOMODE;

    bool bSuccess=false;
    for (int i=0;i<5;i++)
    {
        bSuccess=WriteToCtrlEP(VALUE(nAddress[i],czFreq[i]));
    }
    return bSuccess;
}

// 设置接收机滤波器，单位 MHz
bool TRCtrl::newHF_SetReceiverFilter(double fFreq)
{
    unsigned char chRFfilter=0;
    if(fFreq<2.95) chRFfilter=0;
    else if(fFreq<4.55) chRFfilter=1;
    else if(fFreq<7.15) chRFfilter=2;
    else if(fFreq<11.35) chRFfilter=3;
    else if(fFreq<19.15) chRFfilter=4;
    else if(fFreq<32.35) chRFfilter=5;
    else if(fFreq<58.05) chRFfilter=6;
    else chRFfilter=7;
    //chRFfilter = 7-chRFfilter;
    return WriteToCtrlEP(VALUE(ADDR_OF_RECEIVERFILTER,chRFfilter));
}


// // 0：正交发送+接收；1：正交近场接收；2：正交远场接收；3：正交闭环实验；4：单音发射+接收；5：单音近场接收；6单音远处接收；7：单音闭环测试
void TRCtrl::newHF_ModelCtrl(int iType)
{


    /*if(iType<8)
    {
    newHF_SetRFSwitch(TRUE);


    if(iType==0 || iType==1 || iType==4 || iType== 5)
    {
    newHF_SetTRSwitch(TRUE,TRUE);
    }
    else newHF_SetTRSwitch(false,false);
    }
    else newHF_SetRFSwitch(false);


    if ((iType==0) || (iType==1) || (iType==2) || (iType==3))
    {
    newHF_SetRFFreq(0.0,2);
    newHF_SetLOFreq(0.0,1);

    }
    else
    {
    newHF_SetRFFreq(0.0,1);
    newHF_SetLOFreq(0.0,1);

    }*/
    if(iType<8)
    {



        if(iType==0 )
        {
            newHF_SetRFSwitch(TRUE);
            newHF_SetTRSwitch(TRUE,TRUE);
        }
        else if(iType==3)//HF627BS_SetTRSwitch(false,false);
        {
            newHF_SetRFSwitch(TRUE);
            newHF_SetTRSwitch(TRUE,false);
        }
        if(iType==2)
        {
            newHF_SetRFSwitch(TRUE);
            newHF_SetTRSwitch(false,false);
        }
        if(iType==1)
        {
            newHF_SetRFSwitch(TRUE);
            newHF_SetTRSwitch(false,TRUE);
        }

    }
    else newHF_SetRFSwitch(false);


    if ((iType==0) || (iType==1) || (iType==2) || (iType==3))
    {
        newHF_SetRFFreq(0.0,2);
        newHF_SetLOFreq(0.0,1);

    }
    else
    {
        newHF_SetRFFreq(0.0,1);
        newHF_SetLOFreq(0.0,1);

    }


}

//用来对接收通道射频前端进行压制,一个控制位，通过USB板实现；
bool TRCtrl::newHF_SetRFSwitch(bool bRFSw)
{
    unsigned char mTRen=0;
    if(bRFSw) mTRen=1;
    return WriteToCtrlEP(VALUE(ADDR_OF_RFSW,mTRen));
 }


//射频前端压制开关newHF_SetRFSwitch(true),bTRSw控制射频前端压制开关，LOSw控制本振信号输出至混频器
bool TRCtrl::newHF_SetTRSwitch(bool bTRSw , bool bLOSw)
{
    unsigned char tmpdat[2];
    if(bTRSw) tmpdat[0] = 0x0f; //MSW0161 * 2
    else tmpdat[0]=0x00;


    if (bLOSw) tmpdat[1] = 1; //GPS board for LO1 TR
    else tmpdat[1] = 0x00;
    bool bSuccess=false;
    bSuccess=WriteToCtrlEP(VALUE(ADDR_OF_RECEIVERSW,tmpdat[0]));
    bSuccess=WriteToCtrlEP(VALUE(ADDR_OF_LOSW,tmpdat[1]));
    return bSuccess;
}
